﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Anthem;
using spUtils;
using spMapping;
using spData.Common;


namespace demo.Page.WY
{
    public partial class wy_Fhtfk : TaskPage
    {
        #region Property
        public string isCssShow
        {
            get { return ViewState["isCssShow"] == null ? "" : (string)ViewState["isCssShow"]; }
            set { ViewState["isCssShow"] = value; }
        }

        //初始化下拉
        spWeb.DDLBind db = new spWeb.DDLBind();
        #endregion

        #region Method Init Controls Value & Show/Hide & Enable/Disable & Opinion
        private void InitControls()
        {
            #region Init controls value
            Expression exp = new Expression();
            string did = Request.QueryString["DID"];
            exp = string.IsNullOrEmpty(did) ? dc_wy_Fhtfk.process == TaskAPI.Process & dc_wy_Fhtfk.incident == TaskAPI.Incident : dc_wy_Fhtfk.id == did;
            if (TaskAPI.IsInitiate)
            {
                exp &= dc_wy_Fhtfk.userName == USER.UserName;
            }

            dc_wy_Fhtfk v = exp.ListFirst<dc_wy_Fhtfk>();
            if (v != null)
            {
                SetControlsValue(v);
                DepEName.Text = v.Department;
                //DID方式加载
                if (!string.IsNullOrEmpty(did))
                {
                    TaskAPI.LoadByDID(v.Process, v.Incident);
                    Process = v.Process;
                    Incident = v.Incident;
                }
            }
            else
            {
                UserFullName.Text = USER.UserFullName;
                Time.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                Department.Text = USER.Department;
                DepEName.Text = USER.DepEName;
                Tel.Text = USER.EXTEND.CellPhone;
            }
            #endregion

            #region Init Show/Hide、Enable/Disable
            //bool flg = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;
            bool flg = true;
            SetEnable(flg, Subject, ZFDW, FKNR, FKDX, FKLX, SSFJ, JSJE, JSJEDX,
 LJYFHTJE, LJYFHTJEDX, NZFFS, PJLXHHM, HTLXZK, BCSQFKJE, BCSQFKJEDX, BCQRFKJE, BCQRFKJEDX, SFYZF);
            SetVisible(flg, R_Subject, R_FKDX, R_FKLX, R_BCSQFKJE);
            #endregion
        }
        #endregion

        #region Method Data Validation
        private List<string> DataValidation()
        {
            List<Regular> regs = new List<Regular>();
            if (!AssExt.IsConferWithBack && !AssExt.IsForwardWithBack)
            {
                if (TaskAPI.IsBeginStep)
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, Subject.Text, "“主题事项”为必填项!"));
                    regs.Add(new Regular(RegularExp.NotEmpty, FKDX.Text, "“付款对象”为必填项!"));
                    regs.Add(new Regular(RegularExp.NotEmpty, FKLX.Text, "“付款类型”为必填项!"));
                    regs.Add(new Regular(RegularExp.Float, JSJE.Text, "“决算(结算)金额”应为数字!"));               
                    regs.Add(new Regular(RegularExp.Float, LJYFHTJE.Text, "“累计已付合同金额”应为数字!"));
                    regs.Add(new Regular(RegularExp.Float, BCSQFKJE.Text, "“本次申请付款金额金额小写”应为数字!"));
                    regs.Add(new Regular(RegularExp.Float, BCQRFKJE.Text, "“本次确认付款金额金额小写”应为数字!"));
                }
            }
            return Validation(regs);
        }
        #endregion

        #region Method Engine Transfer Value
        private void EngineTransferValue()
        {
            if (TaskAPI.IsBeginStep)
            {
                TaskAPI.Summary = Subject.Text;
                //spWeb.WebHelper.IntSetValue(this.TaskAPI, Gsgs.SelectedValue, Xmmc.SelectedValue);
            }
            TaskAPI["e_Approve"] = AssExt.Assessment.Opinion;
        }

        #endregion

        #region Method Submit
        private bool Submit(bool IsSend)
        {
            #region “开始”步骤
            if (TaskAPI.IsBeginStep)
            {
                dc_wy_Fhtfk v = (dc_wy_Fhtfk.process == TaskAPI.Process & dc_wy_Fhtfk.incident == TaskAPI.Incident
                                & dc_wy_Fhtfk.userName == (TaskAPI.IsInitiate ? USER.UserName : TaskAPI.UserName)).ListFirst<dc_wy_Fhtfk>();
                bool result = GetControlsValue<dc_wy_Fhtfk>(ref v);
                if (result)
                {
                    v.Process = TaskAPI.Process;
                    v.Incident = TaskAPI.Incident;
                    v.UserName = USER.UserName;
                    v.UserFullName = USER.UserFullName;
                    v.Time = System.DateTime.Now;
                    ApplicationNo.Text = GetNewSequence("DCHT-MZBHT-" + DateTime.Now.ToString("yyyyMMdd") + "-", "", 3);
                    v.ApplicationNo = ApplicationNo.Text;
                }
                ORM.Save(v);
            }
            #endregion

            if (IsSend)
            {
                EngineTransferValue();
                ORM.Save(AssExt.Assessment);
                TaskAPI.Send();
            }

            if (IsSend && TaskAPI.IsInitiate)
            {
                (dc_wy_Fhtfk.process == TaskAPI.Process & dc_wy_Fhtfk.incident == 0 & dc_wy_Fhtfk.userName == USER.UserName).Edit<dc_wy_Fhtfk>(p => p.Incident = TaskAPI.Incident);
            }

            //退出
            if (IsSend)
            {
                Exit();
            }
            return true;
        }
        #endregion

        #region Event Page
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControls();
            }
        }
        #endregion

        #region Event Button Click

        #region Event Save As Draft
        protected void SaveAsDraft_Click(object sender, EventArgs e)
        {
            bool success = Submit(false);
            Anthem.Manager.AddScriptForClientSideEval(string.Format("JSInfomation('{0}')", success ? "保存成功!" : "保存失败!"));
        }
        #endregion

        #region Event Send
        protected void Send_Click(object sender, EventArgs e)
        {
            List<string> msg = DataValidation();
            if (msg.Count > 0)
            {
                Anthem.Manager.AddScriptForClientSideEval(Alert(msg));
                return;
            }

            bool success = Submit(true);
            (sender as spWeb.Controls.AssessmentExt).Refresh();
            Anthem.Manager.AddScriptForClientSideEval(string.Format("JSInfomation('{0}');{1}", success ? "提交成功!" : "提交失败!", success ? "window.close();" : ""));
        }
        #endregion

        #region Event Aborted
        protected void Aborted_Click(object sender, EventArgs e)
        {
            (dc_wy_Fhtfk.process == TaskAPI.Process & dc_wy_Fhtfk.incident == TaskAPI.Incident).Edit<dc_wy_Fhtfk>(p => { p.Status = 4; });
        }
        #endregion

        #endregion

        #region (大写转换)
        protected void HTJE_TextChanged(object sender, EventArgs e)
        {
            System.Web.UI.WebControls.TextBox txtBox = sender as System.Web.UI.WebControls.TextBox;
            if (txtBox != null)
            {
                switch (txtBox.ID)
                {
                    case "JSJE":
                        JSJEDX.Text = Utils.TransformNumberToChina(JSJE.Text.Replace(",", ""));
                        break;

                    case "LJYFHTJE":
                        LJYFHTJEDX.Text = Utils.TransformNumberToChina(LJYFHTJE.Text.Replace(",", ""));
                        break;

                    case "BCSQFKJE":
                        BCSQFKJEDX.Text = Utils.TransformNumberToChina(BCSQFKJE.Text.Replace(",", ""));
                        break;

                    case "BCQRFKJE":
                        BCQRFKJEDX.Text = Utils.TransformNumberToChina(BCQRFKJE.Text.Replace(",", ""));
                        break;

                    case "YJZJE":
                        YJZJEDX.Text = Utils.TransformNumberToChina(YJZJE.Text.Replace(",", ""));
                        break;

                    case "LZBJ":
                        LZBJDX.Text = Utils.TransformNumberToChina(LZBJ.Text.Replace(",", ""));
                        break;

                    case "YFSFHTJE":
                        YFSFHTJEDX.Text = Utils.TransformNumberToChina(YFSFHTJE.Text.Replace(",", ""));
                        break;

                    case "YQYJSJE":
                        YQYJSJEDX.Text = Utils.TransformNumberToChina(YQYJSJE.Text.Replace(",", ""));
                        break;

                    case "MBJEZZ":
                        MBJEZZDX.Text = Utils.TransformNumberToChina(MBJEZZ.Text.Replace(",", ""));
                        break;
                }

            }
        }
        #endregion

    }
}